class AcGamePlayground {
    constructor (root){
        this.root = root;
        this.$playground = $(`
<div class="ac-game-playground"></div>
            `);
        this.hide();

        this.root.$ac_game.append(this.$playground);
        this.start();
    }
    create_uuid() {
        let res = "";
        for (let i = 0; i < 8; i ++ ) {
            let x = parseInt(Math.floor(Math.random() * 10));  // 返回[0, 1)之间的数
            res += x;
        }
        return res;
    }


    start(){
        var that = this;
        // 当用户改变窗口大小时会触发
        let uuid = this.create_uuid();
        $(window).on(`resize.${uuid}`,function(){
            that.resize();
        })

        if(this.root.AcWingOS){
            this.root.AcWingOS.api.window.on_close(function(e){
                $(window).off(`resize.${uuid}`);
            });
        }
    }

    get_random_color(){
        let colors = ["blue","pink","red","green","grey","purple","brown","yellow","Meat color","#ECFF1E"]
        return colors[Math.floor(Math.random()*(colors.length-1))]
    }
    update(){

    }

    end_game(){
        this.state = "over";
        this.scoreborad.lose();
    }

    resize(){
        this.width = this.$playground.width();
        this.height = this.$playground.height();
        let unit = Math.min(this.width / 16 , this.height/9) ;
        this.width = unit * 16;
        this.height = unit * 9;
        this.scale = this.height; // 用高度表示标准

        if(this.game_map) this.game_map.resize();
    }
    show(mode){  //打开playground界面
        var that = this;
        this.$playground.show();

        this.width = this.$playground.width();
        this.height = this.$playground.height();
        this.game_map = new GameMap(this);
        this.mode = mode;
        this.state = "waiting"  // waiting -> fighting -> over

        this.noticeborad = new NoticeBorad(this);
        this.scoreborad = new ScoreBorad(this);
        this.player_count = 0;

        this.resize();
        this.players = [];
        this.players.push(new Player(this, this.width /2 / this.scale , 0.5 , 0.05 , "white" , 0.15 , "me" , this.root.settings.username , this.root.settings.photo));
        if(mode === "single mode"){
            for(var i = 0 ; i < 9 ; i++){
                this.players.push(new Player(this, this.width /2 / this.scale, 0.5 , 0.05 , this.get_random_color() , 0.15 , "robot"));
            }
        }else if (mode === "multi mode"){
            this.chat_field = new ChatField(this);
            this.mps = new MultiPlayerSocket(this);
            this.mps.uuid = this.players[0].uuid;
            // 创建成功的时候返回函数
            this.mps.ws.onopen = function(){
                that.mps.send_create_player(that.root.settings.username, that.root.settings.photo);
            }
        }
    }

    hide(){  //关闭playground页面
        while(this.players && this.players.length > 0){
            this.players[0].distory();
        }

        if(this.game_map){
            this.game_map.distory();
            this.game_map = null;
        }

        if(this.noticeborad){
            this.noticeborad.distory();
            this.boticeborad = null;
        }

        if(this.scoreborad){
            this.scoreborad.distory();
            this.scoreborad = null;
        }

        this.$playground.empty();
        this.$playground.hide();
    }
}
